home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / ARMTEX / SOURCES2 / !TeX / texmf / source / armTeX / web2c / c / splitup < prev    next >
Encoding:
Text File  |  1998-03-16  |  4.5 KB  |  164 lines

  1. /* splitup -- take TeX or MF in C as a single stream on stdin,
  2.    and it produces several .c and .h files in the current directory
  3.    as its output.
  4.  
  5.   Tim Morgan  September 19, 1987.  */
  6.  
  7. #include "config.h"
  8.  
  9. int filenumber = 0, ifdef_nesting = 0, lines_in_file = 0;
  10. char *output_name = "tex";
  11. int has_ini;
  12.  
  13. #ifdef RISCOS
  14. #define TEMPFILE        "c.temp"
  15. #else
  16. #define TEMPFILE        "temp.c"
  17. #endif
  18.  
  19. /* This was 2000, but for efficiency... */
  20. #define MAXLINES        10000
  21.  
  22. #ifdef RISCOS /* don't have unlink() but remove() */
  23. #define unlink remove
  24. #endif
  25. char buffer[1024], filename[PATH_MAX];
  26.  
  27. FILE *out, *ini, *temp;
  28. FILE *in = stdin;
  29.  
  30.  
  31. int
  32. main (argc, argv)
  33.   int argc;
  34.   char *argv[];
  35. {
  36.     if (argc > 1)
  37.         output_name = argv[1];
  38.  
  39. #ifdef RISCOS
  40.     (void) sprintf (filename, "h.%sd", output_name);
  41. #else
  42.     (void) sprintf (filename, "%sd.h", output_name);
  43. #endif
  44.     if (!(out = fopen (filename, "w")))
  45.         FATAL_PERROR (filename);
  46.  
  47.     (void) fprintf(out,
  48.                 "#undef\tTRIP\n#undef\tTRAP\n#define\tSTAT\n#undef\tDEBUG\n");
  49.     for ((void) fgets(buffer, sizeof(buffer), in);
  50. #ifdef RISCOS
  51.           strncmp(&buffer[10], "h.coerce", 8);
  52. #else
  53.           strncmp(&buffer[10], "coerce.h", 8);
  54. #endif
  55.          (void) fgets(buffer, sizeof(buffer), in))
  56.       {
  57.         if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '}'
  58.             || buffer[0] == '/'
  59.             || buffer[0] == ' ' || strncmp(buffer, "typedef", 7) == 0)
  60.           /*nothing*/;
  61.         else
  62.           (void) fputs("EXTERN ", out);
  63.  
  64.         (void) fputs(buffer, out);
  65.       }
  66.  
  67.     (void) fputs(buffer, out);
  68.     fclose (out);
  69.  
  70. #ifdef RISCOS
  71.     (void) sprintf(filename, "c.i%s", output_name);
  72. #else
  73.     (void) sprintf(filename, "i%s.c", output_name);
  74. #endif
  75.     ini = fopen(filename, "w");
  76.     if (!ini)
  77.         FATAL_PERROR (filename);
  78.  
  79.     (void) fputs("#define EXTERN extern\n", ini);
  80.     (void) fprintf(ini, "#include \"%sd.h\"\n\n", output_name);
  81. #ifdef RISCOS
  82.     (void) sprintf(filename, "c.%s0", output_name);
  83. #else
  84.     (void) sprintf(filename, "%s0.c", output_name);
  85. #endif
  86.  
  87.     if (!(out = fopen(filename, "w")))
  88.         FATAL_PERROR (filename);
  89.     (void) fputs("#define EXTERN extern\n", out);
  90.     (void) fprintf(out, "#include \"%sd.h\"\n\n", output_name);
  91.  
  92.     do {
  93.         /* Read one routine into a temp file */
  94.         has_ini = false;
  95.         if (!(temp = fopen(TEMPFILE, "w+")))
  96.             FATAL_PERROR (TEMPFILE);
  97.             
  98.         while (read_line()) {
  99.             (void) fputs(buffer, temp);
  100.             if (buffer[0] == '}') break; /* End of procedure */
  101.         }
  102.         while (ifdef_nesting > 0 && read_line())
  103.             (void) fputs(buffer, temp);
  104.         rewind(temp);
  105.  
  106.         if (has_ini) {  /* Contained "#ifdef INI..." */
  107.             while (fgets(buffer, sizeof(buffer), temp))
  108.                 (void) fputs(buffer, ini);
  109.         }
  110.         else {                  /* Doesn't contain "#ifdef INI..." */
  111.             while (fgets(buffer, sizeof(buffer), temp)) {
  112.                 (void) fputs(buffer, out);
  113.                 lines_in_file++;
  114.             }
  115.         }
  116.         if (fclose (temp))
  117.             FATAL_PERROR ("fclose");
  118.             
  119.         if (lines_in_file > MAXLINES) {
  120.             if (fclose(out))
  121.                 perror("fclose"), uexit (1);
  122. #ifdef RISCOS
  123.             (void) sprintf(filename, "c.%s%d", output_name, ++filenumber);
  124. #else
  125.             (void) sprintf(filename, "%s%d.c", output_name, ++filenumber);
  126. #endif
  127.             if ( !(out = fopen(filename, "w")))
  128.                 perror(filename), uexit (1);
  129.             (void) fputs("#define EXTERN extern\n", out);
  130.             (void) fprintf(out, "#include \"%sd.h\"\n\n", output_name);
  131.             lines_in_file = 0;
  132.         }
  133.     } while (!feof(in));
  134.  
  135.     if (fclose(out))
  136.         perror("fclose"), uexit (1);
  137.     if (lines_in_file == 0)
  138.         (void) unlink(filename);
  139.     if (fclose(ini))
  140.         perror("fclose"), uexit (1);
  141.     if (unlink(TEMPFILE))
  142.         perror(TEMPFILE), uexit (1);
  143.  
  144.     uexit (0);
  145. }
  146.  
  147. /*
  148.  * Read a line of input into the buffer, returning `false' on EOF.
  149.  * If the line is of the form "#ifdef INI...", we set "has_ini"
  150.  * `true' else `false'.  We also keep up with the #ifdef/#endif nesting
  151.  * so we know when it's safe to finish writing the current file.
  152.  */
  153. int
  154. read_line()
  155. {
  156.     if (fgets(buffer, sizeof(buffer), in) == NULL) return false;
  157.     if (strncmp(buffer, "#ifdef", 6) == 0) {
  158.         ++ifdef_nesting;
  159.         if (strncmp(&buffer[7], "INI", 3) == 0) has_ini = true;
  160.     }
  161.     else if (strncmp(buffer, "#endif", 6) == 0) --ifdef_nesting;
  162.     return true;
  163. }
  164.